package com.bitcola.exchange.push.http;


import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.bitcola.exchange.push.config.EmailConstant;


import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 发送邮件
 *
 *  @author wx
 * @create 2018-10-13 15:03
 **/
public class EmailUtils {

    public static final String APP_ID = "15114";
    public static final String APP_KEY = "f05be9fbb018c8c1b10ad1cfb75f1182";

    public static final String SEND_CLOUD_USER = "dogexplus";

    public static final String SEND_CLOUD_KEY = "DveypCKipEuk4t3F";

    public static final String SEND_CLOUD_URL = "https://api.sendcloud.net/apiv2/mail/send";

    /**
     * 发送邮件sendcloud
     * @param language
     * @param to
     * @param code
     * @param antiPhishingCode
     */
    public static void sendCloud(String language,String to,String code,String antiPhishingCode){
        Map<String, Object> map = new HashMap();
        map.put("apiUser", SEND_CLOUD_USER);
        map.put("apiKey", SEND_CLOUD_KEY);
        map.put("from", "firebull@m.firebull.cc");
        map.put("fromName","FIREBULL");
        map.put("to", to);
        if(language.equals("CN")){
            map.put("subject", "FIREBULL验证码");
            map.put("html",String.format(EmailConstant.content, "FIREBULL验证码",code,"感谢选择火牛"));
        }else{
            map.put("subject", "huodun verification code");
            map.put("html",String.format(EmailConstant.content, "FIREBULL verification code",code,"Thank you for choosing FIREBULL "));
        }
        String response = HttpUtil.post( SEND_CLOUD_URL, map);
        Map<String, Object> rw = JSONObject.parseObject( response, Map.class);
        if( !(boolean) rw.get("result")){
            System.out.println("邮件发送给 : "+to + " 验证码 : "+code + " 结果: "+ rw.get("message"));
        } else {
            System.out.println("邮件发送给 : "+to + " 验证码 : "+code + " 结果: "+ rw.get("message"));
        }
    }


    public static void sendEmailContent(String to,String title,String content){
        String URL = "https://api.mysubmail.com/mail/send.json";
        HashMap<String, String> paramer = new HashMap<String, String>();
        paramer.put("appid", APP_ID);
        paramer.put("signature", APP_KEY);
        paramer.put("to", to);
        paramer.put("from", "m.firebull.cc");
         content=String.format(EmailConstant.content, title,content);;

        paramer.put("html",content);

        String result = executePostByUsual(URL, paramer);
//        System.out.println("邮件发送给 : "+to + " 验证码 : "+code + " 结果: "+result);
    }



    public static String executePostByUsual(String actionURL, HashMap<String, String> parameters) {
        String response = "";
        try {
            URL url = new URL(actionURL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 发送post请求需要下面两行
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Charset", "UTF-8");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            // 设置请求数据内容
            String requestContent = "";
            Set<String> keys = parameters.keySet();
            for (String key : keys) {
                requestContent = requestContent + key + "=" + URLEncoder.encode(parameters.get(key), "UTF-8") + "&";
            }
            requestContent = requestContent.substring(0, requestContent.lastIndexOf("&"));
            DataOutputStream ds = new DataOutputStream(connection.getOutputStream());
            // 使用write(requestContent.getBytes())是为了防止中文出现乱码
            ds.write(requestContent.getBytes());
            ds.flush();
            try {
                // 获取URL的响应
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
                String s = "";
                String temp = "";
                while ((temp = reader.readLine()) != null) {
                    s += temp;
                }
                response = s;
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("No response get!!!");
            }
            ds.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Request failed!");
        }
        return response;
    }


    public static void main(String[] args) {
        sendCloud("CN","120443910@qq.com","验证码","8761424");
    }


}
